1 using UnityEngine;
2 using
UnityEngine.SceneManagement;
3 using
System.Collections;
4
5 namespace
Completed
6 {
7     
using System.Collections.Generic; //Allows us to use Lists.
8     
using UnityEngine.UI; //Allows us to use UI.
9     
10     
public class GameManager : MonoBehaviour
11     {
12         
public float levelStartDelay = 2f; //Time to wait before starting level, in seconds.
13         
public float turnDelay = 0.1f; //Delay between each Player turn.
14         
public int playerFoodPoints = 100; //Starting value for Player food points.
15         
public static GameManager instance = null; //Static instance of GameManager which allows it to be accessed by any other script.
16         [HideInInspector]
public bool playersTurn = true; //Boolean to check if it's players turn, hidden in inspector but public.
17         
18         
19         
private Text levelText; //Text to display current level number.
20         
private GameObject levelImage; //Image to block out level as levels are being set up, background for levelText.
21         
private BoardManager boardScript; //Store a reference to our BoardManager which will set up the level.
22         
private int level = 1; //Current level number, expressed in game as "Day 1".
23         
private List<Enemy> enemies; //List of all Enemy units, used to issue them move commands.
24         
private bool enemiesMoving; //Boolean to check if enemies are moving.
25         
private bool doingSetup = true; //Boolean to check if we're setting up board, prevent Player from moving during setup.
26         
27         
28         
29         
//Awake is always called before any Start functions
30         
void Awake()
31         {
32             
//Check if instance already exists
33             
if (instance == null)
34
35                 
//if not, set instance to this
36                 instance =
this;
37
38             
//If instance already exists and it's not this:
39             
else if (instance != this)
40
41                 
//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
42                 Destroy(gameObject);
43             
44             
//Sets this to not be destroyed when reloading scene
45             DontDestroyOnLoad(gameObject);
46             
47             
//Assign enemies to a new List of Enemy objects.
48             enemies =
new List<Enemy>();
49             
50             
//Get a component reference to the attached BoardManager script
51             boardScript = GetComponent<BoardManager>();
52             
53             
//Call the InitGame function to initialize the first level
54             InitGame();
55         }
56
57         
//this is called only once, and the paramter tell it to be called only after the scene was loaded
58         
//(otherwise, our Scene Load callback would be called the very first load, and we don't want that)
59         
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
60         
static public void CallbackInitialization()
61         {
62             
//register the callback to be called everytime the scene is loaded
63             SceneManager.sceneLoaded += OnSceneLoaded;
64         }
65
66         
//This is called each time a scene is loaded.
67         
static private void OnSceneLoaded(Scene arg0, LoadSceneMode arg1)
68         {
69             instance.level++;
70             instance.InitGame();
71         }
72
73         
74         
//Initializes the game for each level.
75         
void InitGame()
76         {
77             
//While doingSetup is true the player can't move, prevent player from moving while title card is up.
78             doingSetup =
true;
79             
80             
//Get a reference to our image LevelImage by finding it by name.
81             levelImage = GameObject.Find(
"LevelImage");
82             
83             
//Get a reference to our text LevelText's text component by finding it by name and calling GetComponent.
84             levelText = GameObject.Find(
"LevelText").GetComponent<Text>();
85             
86             
//Set the text of levelText to the string "Day" and append the current level number.
87             levelText.text =
"Day " + level;
88             
89             
//Set levelImage to active blocking player's view of the game board during setup.
90             levelImage.SetActive(
true);
91             
92             
//Call the HideLevelImage function with a delay in seconds of levelStartDelay.
93             Invoke(
"HideLevelImage", levelStartDelay);
94             
95             
//Clear any Enemy objects in our List to prepare for next level.
96             enemies.Clear();
97             
98             
//Call the SetupScene function of the BoardManager script, pass it current level number.
99             boardScript.SetupScene(level);
100             
101         }
102         
103         
104         
//Hides black image used between levels
105         
void HideLevelImage()
106         {
107             
//Disable the levelImage gameObject.
108             levelImage.SetActive(
false);
109             
110             
//Set doingSetup to false allowing player to move again.
111             doingSetup =
false;
112         }
113         
114         
//Update is called every frame.
115         
void Update()
116         {
117             
//Check that playersTurn or enemiesMoving or doingSetup are not currently true.
118             
if(playersTurn || enemiesMoving || doingSetup)
119                 
120                 
//If any of these are true, return and do not start MoveEnemies.
121                 
return;
122             
123             
//Start moving enemies.
124             StartCoroutine (MoveEnemies ());
125         }
126         
127         
//Call this to add the passed in Enemy to the List of Enemy objects.
128         
public void AddEnemyToList(Enemy script)
129         {
130             
//Add Enemy to List enemies.
131             enemies.Add(script);
132         }
133         
134         
135         
//GameOver is called when the player reaches 0 food points
136         
public void GameOver()
137         {
138             
//Set levelText to display number of levels passed and game over message
139             levelText.text =
"After " + level + " days, you starved.";
140             
141             
//Enable black background image gameObject.
142             levelImage.SetActive(
true);
143             
144             
//Disable this GameManager.
145             enabled =
false;
146         }
147         
148         
//Coroutine to move enemies in sequence.
149         IEnumerator MoveEnemies()
150         {
151             
//While enemiesMoving is true player is unable to move.
152             enemiesMoving =
true;
153             
154             
//Wait for turnDelay seconds, defaults to .1 (100 ms).
155             
yield return new WaitForSeconds(turnDelay);
156             
157             
//If there are no enemies spawned (IE in first level):
158             
if (enemies.Count == 0)
159             {
160                 
//Wait for turnDelay seconds between moves, replaces delay caused by enemies moving when there are none.
161                 
yield return new WaitForSeconds(turnDelay);
162             }
163             
164             
//Loop through List of Enemy objects.
165             
for (int i = 0; i < enemies.Count; i++)
166             {
167                 
//Call the MoveEnemy function of Enemy at index i in the enemies List.
168                 enemies[i].MoveEnemy ();
169                 
170                 
//Wait for Enemy's moveTime before moving next Enemy,
171                 
yield return new WaitForSeconds(enemies[i].moveTime);
172             }
173             
//Once Enemies are done moving, set playersTurn to true so player can move.
174             playersTurn =
true;
175             
176             
//Enemies are done moving, set enemiesMoving to false.
177             enemiesMoving =
false;
178         }
179     }
180 }


using System.Collections.Generic; Allows us to use Lists.

using UnityEngine.UI; Allows us to use UI.

public float levelStartDelay = 2f; Time to wait before starting level, in seconds.

public float turnDelay = 0.1f; Delay between each Player turn.

public int playerFoodPoints = 100; Starting value for Player food points.

public static GameManager instance = null; Static instance of GameManager which allows it to be accessed by any other script.

[HideInInspector] public bool playersTurn = true; Boolean to check if it's players turn, hidden in inspector but public.

private Text levelText; Text to display current level number.

private GameObject levelImage; Image to block out level as levels are being set up, background for levelText.

private BoardManager boardScript; Store a reference to our BoardManager which will set up the level.

private int level = 1; Current level number, expressed in game as "Day 1".

private List enemies; List of all Enemy units, used to issue them move commands.

private bool enemiesMoving; Boolean to check if enemies are moving.

private bool doingSetup = true; Boolean to check if we're setting up board, prevent Player from moving during setup.

Awake is always called before any Start functions

Check if instance already exists

if not, set instance to this

If instance already exists and it's not this:

Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.

Sets this to not be destroyed when reloading scene

Assign enemies to a new List of Enemy objects.

Get a component reference to the attached BoardManager script

Call the InitGame function to initialize the first level

this is called only once, and the paramter tell it to be called only after the scene was loaded

(otherwise, our Scene Load callback would be called the very first load, and we don't want that)

register the callback to be called everytime the scene is loaded

This is called each time a scene is loaded.

Initializes the game for each level.

While doingSetup is true the player can't move, prevent player from moving while title card is up.

Get a reference to our image LevelImage by finding it by name.

Get a reference to our text LevelText's text component by finding it by name and calling GetComponent.

Set the text of levelText to the string "Day" and append the current level number.

Set levelImage to active blocking player's view of the game board during setup.

Call the HideLevelImage function with a delay in seconds of levelStartDelay.

Clear any Enemy objects in our List to prepare for next level.

Call the SetupScene function of the BoardManager script, pass it current level number.

Hides black image used between levels

Disable the levelImage gameObject.

Set doingSetup to false allowing player to move again.

Update is called every frame.

Check that playersTurn or enemiesMoving or doingSetup are not currently true.

If any of these are true, return and do not start MoveEnemies.

Start moving enemies.

Call this to add the passed in Enemy to the List of Enemy objects.

Add Enemy to List enemies.

GameOver is called when the player reaches 0 food points

Set levelText to display number of levels passed and game over message

Enable black background image gameObject.

Disable this GameManager.

Coroutine to move enemies in sequence.

While enemiesMoving is true player is unable to move.

Wait for turnDelay seconds, defaults to .1 (100 ms).

If there are no enemies spawned (IE in first level):

Wait for turnDelay seconds between moves, replaces delay caused by enemies moving when there are none.

Loop through List of Enemy objects.

Call the MoveEnemy function of Enemy at index i in the enemies List.

Wait for Enemy's moveTime before moving next Enemy,

Once Enemies are done moving, set playersTurn to true so player can move.

Enemies are done moving, set enemiesMoving to false.




Trò chơi giống như Rogue 2D sử dụng Unity 28.443 lượt xem

Gõ tìm kiếm nhanh...